//
//  ReachCityCollectionViewController.m
//  WeatherGuide
//
//  Created by lanou3g on 15/12/23.
//  Copyright © 2015年 李明. All rights reserved.
//

#import "ReachCityCollectionViewController.h"
#import "ReachCityCollectionViewCell.h"
#import "GetWeatherHandle.h"
#import "GetDataFromURLTool.h"
#import "Weather.h"

#import "WeatherCollectionViewController.h"
#import "reachCityView.h"
@interface ReachCityCollectionViewController ()
@property (strong, nonatomic) IBOutlet reachCityView *collectionview;

@property (nonatomic,strong) NSMutableArray *cityArray;

@end

@implementation ReachCityCollectionViewController




- (void)viewDidLoad {
    [super viewDidLoad];
    
  
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:@selector(leftBarButtonItemAction:)];
    
    [self.collectionview registerClass:[ReachCityCollectionViewCell class] forCellWithReuseIdentifier:@"ReachCityCollectionViewCell"];
//    self.collectionview.backgroundColor = [UIColor whiteColor];
    // Do any additional setup after loading the view.
}

- (void)leftBarButtonItemAction:(UIBarButtonItem *)sender
{
    [self.navigationController dismissViewControllerAnimated:YES completion:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (NSMutableArray *)cityArray
{
    if (_cityArray == nil) {
        _cityArray = [NSMutableArray array];
    }
    return _cityArray;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

#pragma mark <UICollectionViewDataSource>

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
#warning Incomplete implementation, return the number of sections
    return 1;
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
#warning Incomplete implementation, return the number of items
    
    NSLog(@"---------%ld",self.array.count);
    return self.array.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
//    NSLog(@"111");
    ReachCityCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ReachCityCollectionViewCell" forIndexPath:indexPath];
    
    cell.label.text = self.array[indexPath.row];
    
    return cell;
}

#pragma mark <UICollectionViewDelegate>

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    ReachCityCollectionViewCell *cell = (ReachCityCollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
    
     NSArray *array = [[[GetWeatherHandle sharedHandle].dicCityInfo valueForKey:cell.label.text] allKeys];
    
    if (array.count != 0) {
        self.array = array;
        [collectionView reloadData];
    }else{
        [self dismissViewControllerAnimated:YES completion:^{
            [GetDataFromURLTool getDataFromURLStr:cell.label.text backBlock:^(id object) {
                [[NSNotificationCenter defaultCenter] postNotificationName:@"conllectionViewReloadDataAction" object:cell.label.text];
                
                [[NSNotificationCenter defaultCenter] postNotificationName:@"DelCollectionViewController" object:cell.label.text];
            }];
        }];
    }
  
}


- (void)reachVCDis:(NSNotification *)sender
{

  
}



/*
// Uncomment this method to specify if the specified item should be highlighted during tracking
- (BOOL)collectionView:(UICollectionView *)collectionView shouldHighlightItemAtIndexPath:(NSIndexPath *)indexPath {
	return YES;
}
*/

/*
// Uncomment this method to specify if the specified item should be selected
- (BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}
*/

/*
// Uncomment these methods to specify if an action menu should be displayed for the specified item, and react to actions performed on the item
- (BOOL)collectionView:(UICollectionView *)collectionView shouldShowMenuForItemAtIndexPath:(NSIndexPath *)indexPath {
	return NO;
}

- (BOOL)collectionView:(UICollectionView *)collectionView canPerformAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
	return NO;
}

- (void)collectionView:(UICollectionView *)collectionView performAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
	
}
*/

@end
